<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="设备名称" prop="deviceName">
                <el-input v-model="queryParams.cameraName" placeholder="请输入设备名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>


            <el-form-item label="安装位置" prop="location">
                <el-input v-model="queryParams.location" placeholder="请输入安装位置" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="区域" prop="geoId">
                <el-select v-model="queryParams.geoId" placeholder="请选择安装区域">
                    <el-option v-for="item in geometrys" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>

            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['app:cameras:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['app:cameras:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['app:cameras:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain  size="mini" @click="handleconfig">配置</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain  size="mini" @click="resetEquip">更新</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:device:export']">导出</el-button>
            </el-col> -->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />

            <el-table-column label="设备名称" align="center" prop="cameraName" />
            <!-- <el-table-column label="视频链接" align="center" prop="url" /> -->

            <!-- <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    <p v-if="scope.row.status == 1">启用</p>
                    <p v-if="scope.row.status == 2">禁用</p>
                </template>
            </el-table-column> -->

            <el-table-column label="经度" align="center" prop="x" />
            <el-table-column label="纬度" align="center" prop="y" />
            <el-table-column label="摄像头类型" align="center" prop="cameraTypeName">
                <!-- <template slot-scope="scope">
                    <p v-if="scope.row.status == 1">球机</p>
                    <p v-if="scope.row.status == 2">枪机</p>
                </template> -->
            </el-table-column>
            <el-table-column label="区域" align="center" prop="name" />

            <el-table-column label="安装位置" align="center" prop="location" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['app:cameras:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['app:cameras:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改摄像机位置点位以及链接对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="设备名称" prop="cameraName">
                    <el-input v-model="form.cameraName" placeholder="请输入设备名称" />
                </el-form-item>
                <!-- <el-form-item label="视频链接" prop="url">
                    <el-input v-model="form.url" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="摄像类型">
                    <el-select v-model="form.type" placeholder="请选择是摄像机类型">
                        <el-option label="球机" :value="1"> </el-option>
                        <el-option label="枪击" :value="2"> </el-option>
                    </el-select>

                </el-form-item> -->

                <!-- <el-form-item label="是否启用">
                    <el-select v-model="form.status" placeholder="请选择是启用">
                        <el-option label="启用" :value="1"> </el-option>
                        <el-option label="未启用" :value="2"> </el-option>
                    </el-select>

                </el-form-item> -->
                <el-form-item label="选择点位">
                    <el-button type="info" @click="mapPoint">选择摄像机坐标</el-button>
                </el-form-item>


                <el-form-item label="经度" prop="x">
                    <el-input v-model="form.x" placeholder="请输入经度" />
                </el-form-item>
                <el-form-item label="纬度" prop="y">
                    <el-input v-model="form.y" placeholder="请输入纬度" />
                </el-form-item>
                <el-form-item label="安装位置" prop="location">
                    <el-input v-model="form.location" placeholder="请输入安装位置" />
                </el-form-item>
                <el-form-item label="安装区域" prop="geoId">
                    <el-select v-model="form.geoId" placeholder="请选择安装区域">
                        <el-option v-for="item in geometrys" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>

                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <el-dialog :visible.sync="openMap" fullscreen append-to-body>
            <div style="font-size: 20px;"><span style="margin-right: 20px;">X坐标:{{ form.x }}</span><span>Y坐标:{{
                form.y }}</span></div>
            <div id="openMap"></div>
        </el-dialog>

        <el-dialog :visible.sync="openconfig" fullscreen append-to-body>
            <el-form :model="fope" ref="fope" size="small" :inline="true" v-show="showSearch" label-width="68px">
                <el-form-item label="设备名称" prop="deviceName">
                    <el-input v-model="fope.cameraName" placeholder="请输入设备名称" clearable
                        @keyup.enter.native="handleQueryforp" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryforp">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQueryforp">重置</el-button>
                </el-form-item>
            </el-form>
            <el-table :data="getNotList">
                <!-- <el-table-column type="selection" width="55" align="center" /> -->

                <el-table-column label="设备名称" align="center" prop="cameraName" />
                <!-- <el-table-column label="视频链接" align="center" prop="url" /> -->

                <!-- <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    <p v-if="scope.row.status == 1">启用</p>
                    <p v-if="scope.row.status == 2">禁用</p>
                </template>
            </el-table-column> -->

                <!-- <el-table-column label="经度" align="center" prop="x" />
                <el-table-column label="纬度" align="center" prop="y" /> -->
                <el-table-column label="摄像头类型" align="center" prop="cameraTypeName">
                    <!-- <template slot-scope="scope">
                    <p v-if="scope.row.status == 1">球机</p>
                    <p v-if="scope.row.status == 2">枪机</p>
                </template> -->
                </el-table-column>
                <!-- <el-table-column label="区域" align="center" prop="name" /> -->

                <el-table-column label="安装位置" align="center" prop="location" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateCameras(scope.row)"
                            v-hasPermi="['system:device:edit']">修改</el-button>

                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="totalop > 0" :total="totalop" :page.sync="fope.pageNum" :limit.sync="fope.pageSize"
                @pagination="NotList" />
            <el-dialog width="70%" title="修改" :visible.sync="innerVisiblexy" append-to-body>
                <div id="map" style="width:100%;height:50vh;margin-bottom: 20px;;"></div>
                <el-form ref="ftorm" :model="ftorm" label-width="80px">
                    <el-form-item label="经度" prop="x">
                        <el-input v-model="ftorm.x" placeholder="请输入经度" />
                    </el-form-item>
                    <el-form-item label="纬度" prop="y">
                        <el-input v-model="ftorm.y" placeholder="请输入纬度" />
                    </el-form-item>

                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="submitftorm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>


            </el-dialog>

        </el-dialog>

    </div>
</template>
  
<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice, geometryList, hikvisionCamerasgetNotList,hikvisionCamerasupdate } from "@/api/system/video";


export default {
    name: "Device",
    data() {
        return {
            //区域列表
            geometrys: [],
            // 遮罩层
            loading: true,
            openconfig: false,
            innerVisiblexy: false,
            openMap: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 摄像机位置点位以及链接表格数据
            deviceList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                cameraName: null,
                geoId: null,
                // url: null,
                mineId: null,
                status: null,
                x: null,
                y: null,
                type: null,
                location: null,
                geoId: null
            },
            // 表单参数
            form: {},
            ftorm: {
                x: null,
                y: null,
            },
            // 表单校验
            rules: {
            },
            Map: null,
            getNotList: [],
            fope: {
                pageNum: 1,
                pageSize: 10,
            },
            totalop: 0,
            Map: null
        };
    },
    async created() {
        await this.getList();
        await geometryList().then(res => {
            console.log(res, 'resss');
            this.geometrys = res.data
        })
    },
    methods: {
        popMap() {
            this.Map = new BMap.Map("map", { mapType: BMAP_SATELLITE_MAP }); // 创建Map实例
            this.Map.centerAndZoom(
                new BMap.Point(114.6147429352289, 24.130142523152553),
                16
            ); // 初始化地图,设置中心点坐标和地图级别
            this.Map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放

            this.Map.addEventListener("click", (e) => {
                let { lng, lat } = e.point
                console.log(this);
                this.ftorm.x = lng;
                this.ftorm.y = lat;
                // console.log(e.point.lng + "," + e.point.lat);
            });
        },
        submitftorm() {
            this.$refs["ftorm"].validate(valid => {
                if (valid) {
                    if (this.ftorm.id != null) {
                        updateDevice(this.ftorm).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.openconfig = false;
                            this.NotList();
                            this.getList();
                        });
                    }
                }
            });
        },
        handleUpdateCameras(v) {
            this.ftorm.id = v.id;
        
            this.innerVisiblexy = true;
            this.$nextTick(() => {
                this.popMap();
            })

        },
        handleconfig(e) {
            this.openconfig = true;
            this.NotList();
        },
        NotList() {
            hikvisionCamerasgetNotList(this.fope).then(res => {
                this.getNotList = res.rows;
                this.totalop = res.total;
            })
        },

        mapPoint() {

            this.openMap = true
            this.$nextTick(() => {
                this.Map = new BMap.Map("openMap", { mapType: BMAP_SATELLITE_MAP }); // 创建Map实例

                console.log(this.Map);
                this.Map.centerAndZoom(
                    new BMap.Point(114.608347, 24.120331),
                    16
                ); // 初始化地图,设置中心点坐标和地图级别
                this.Map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放
                this.Map.addEventListener("click", (e) => {
                    // console.log(e.point.lng + "," + e.point.lat);
                    this.Map.clearOverlays();
                    this.form.x = e.point.lng;
                    this.form.y = e.point.lat;
                    let pitA = new BMap.Point(e.point.lng, e.point.lat);
                    var myIcon = new BMap.Icon("https://ryansystem-bucket.oss-cn-shenzhen.aliyuncs.com/upload/video.png", new BMap.Size(50, 50));
                    myIcon.setImageSize(new BMap.Size(50, 50));
                    var marker = new BMap.Marker(pitA, {
                        icon: myIcon,
                    });
                    this.Map.addOverlay(marker);

                });
            })

        },
        /** 查询摄像机位置点位以及链接列表 */
        async getList() {
            this.loading = true;
            await listDevice(this.queryParams).then(response => {
                this.deviceList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.innerVisiblexy = false;
            this.openconfig = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                deviceName: null,
                url: null,
                mineId: null,
                status: null,
                x: null,
                y: null,
                type: null,
                location: null
            };
            this.resetForm("form");
        },
        // 更新设备
        resetEquip(){
            this.loading = true
            hikvisionCamerasupdate().then(res=>{
                console.log(res);
                if (res.code == 200) {
                    this.loading = false
                    this.$modal.msgSuccess("更新成功");
                }else {
                    this.loading = false
                    this.$message.error("更新超时");
                }
            })
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.currentPage = 1;
            this.getList();
        },
        handleQueryforp() {
            this.fope.pageNum = 1;
            this.NotList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        resetQueryforp() {
            this.fope.cameraName = null;
            this.handleQueryforp();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加摄像机位置点位以及链接";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getDevice(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改摄像机位置点位以及链接";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateDevice(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addDevice(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除摄像机位置点位以及链接编号为"' + ids + '"的数据项？').then(function () {
                return delDevice(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/device/export', {
                ...this.queryParams
            }, `device_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
<style scoped lang="scss">
#openMap {
    width: 100%;
    height: 100vh;
}
</style>
  